from django.shortcuts import render, redirect, reverse

from item.models import Category, Item
from .forms import SignupForm


def home(request):
    items = Item.objects.filter(is_sold=False)[0:6]
    categories = Category.objects.all()

    return render(request, 'shop/index.html', {
        'categories': categories,
        'items': items,
    })


def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)

        if form.is_valid():
            form.save()

            return redirect(reverse('shop:login'))
    else:
        form = SignupForm()

    return render(request, 'shop/signup.html', {
        'form': form
    })
